package suncertify.gui.panels;

import java.awt.FlowLayout;
import java.awt.event.KeyEvent;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import suncertify.application.Strings;

/**
 * Provides a panel that consists of a {@link JLabel} and {@link JTextField} components to enter the computer name
 * running the server application. The label and text field are aligned to the left.
 * 
 */
public class ServerNamePanel extends JPanel {

    /**
     * Automatically generated serialization version ID.
     */
    private static final long serialVersionUID = 2609775527932108248L;

    /**
     * Declares the label component.
     */
    private JLabel nameLabel;

    /**
     * Declares the computer name text field.
     */
    private JTextField nameTextField;

    /**
     * Constructs the class with the specified parameter. The panel layout is set to the left. The label's text is set
     * to {@link Strings#CAP_SERVER_NAME}. The text field's text is set to the <code>serverName</code> and assigned a
     * tool tip. The parameter value is not validated and is used as it is.
     * 
     * @param serverName
     *            Provides the default content of the text field. This value is usually the name of the server computer
     *            that was stored previously in the applications properties file.
     * 
     * @throws NullPointerException
     *             If the <code>serverName</code> is null.
     */
    public ServerNamePanel(String serverName) {

	if (serverName == null) {
	    throw new NullPointerException("ServerNamePanel cannot be constructed with null serverName parameter!");
	}

	this.setLayout(new FlowLayout(FlowLayout.LEFT));

	this.nameLabel = new JLabel(Strings.CAP_SERVER_NAME);
	this.nameLabel.setDisplayedMnemonic(KeyEvent.VK_N);

	this.nameTextField = new JTextField(30);
	this.nameTextField.setText(serverName);
	this.nameTextField.setToolTipText(Strings.TIP_SERVER_NAME);

	this.nameLabel.setLabelFor(this.nameTextField);

	this.add(this.nameLabel);
	this.add(this.nameTextField);

    }

    /**
     * Returns the server name from the text field. The server name is trimmed and not validated in any form.
     * 
     * @return The server name.
     */
    public String getServerName() {

	return this.nameTextField.getText().trim();
    }
}
